const http = require('http')
const Controller = require('./controller')

const server = http.createServer()
const controller = new Controller()

server.on('request', async (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Headers", "*");
  res.setHeader('Content-Type', 'application/json')

  if (req.method === 'OPTIONS') {
    res.statusCode = 200
    res.end()
    return
  }

  if (req.url === '/verify') {
    await controller.verifyUploadFile(req, res)
    return
  }

  if (req.url === '/upload') {
    await controller.uploadFile(req, res)
    return
  }

  if (req.url === '/merge') {
    await controller.mergeFile(req, res)
    return
  }
})

server.listen(3000, () => {
  console.log('listen http://localhost:3000/')
})